package evs.testactivation;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import evs.brp.RemotingErrorException;

public class TestClient {
	
	private Log log = LogFactory.getLog(TestClient.class);
	
	public void printSyntax() {
		System.err.println("Syntax: TestClient ServerURI");
		System.exit(-1);
	}	
	
	public static void main(String[] args) {
		TestClient testClient = new TestClient();
		
		if(args.length != 1)
			testClient.printSyntax();
		
		testClient.runTestSuite(args[0]);
	}
	
	/**
	 * 
	 * +++++++++++++++++++++++++++++++
	 * 
	 * if testclient is run multiple times -> counters get higher because currently no sort of lifecycle management / garbage collector is implemented
	 * 
	 * +++++++++++++++++++++++++++++++
	 * 
	 */
	public void runTestSuite(String serverUri) {
		
		ProxyA proxy_A_1 = new ProxyA();
		ProxyA proxy_A_2 = new ProxyA();
		ProxyB proxy_B = new ProxyB();
		ProxyB proxy_B_2 = new ProxyB();
		
		try {
			log.info("------ before");
			proxy_A_1.bind(new URI(serverUri + "/Object_A_1"));
			proxy_A_2.bind(new URI(serverUri + "/Object_A_2"));
			proxy_B.bind(new URI(serverUri + "/Object_B_1"));
			proxy_B_2.bind(new URI(serverUri + "/Object_B_2"));
			log.info("------ after");			
			proxy_A_1.increment();
			proxy_A_1.increment();
			log.info("****************** invoked increment of instance 1 of A 2 x");
			
			proxy_A_2.increment();
			proxy_A_2.increment();
			proxy_A_2.increment();
			log.info("****************** invoked increment of instance 2 of A 3 x");
			
			proxy_B.increment();
			proxy_B.increment();
			log.info("****************** invoked increment of instance 1 of B (static) 2 x");
			
			proxy_B_2.increment();
			proxy_B_2.increment();
			proxy_B_2.increment();
			log.info("****************** invoked increment of instance 2 of B (static) 3 x");
			

			
			Integer currCount = null;
			int myCount = 0;
			
			currCount = null;
			myCount = 0;
			currCount = proxy_A_1.getCounter();
			if(currCount != null) myCount = currCount.intValue();
			log.info("****************** count of instance 1 of A: " + myCount);
			
			currCount = null;
			myCount = 0;
			currCount = proxy_A_2.getCounter();
			if(currCount != null) myCount = currCount.intValue();
			log.info("****************** count of instance 2 of A: " + myCount);
			
			currCount = null;
			myCount = 0;
			currCount = proxy_B.getCounter();
			if(currCount != null) myCount = currCount.intValue();
			log.info("****************** count of instance 1 of (static) B: " + myCount);
			
			currCount = null;
			myCount = 0;
			currCount = proxy_B_2.getCounter();
			if(currCount != null) myCount = currCount.intValue();
			log.info("****************** count of instance 2 of (static) B: " + myCount);
			
			
			
			proxy_A_1.unbind();
			proxy_A_2.unbind();
			proxy_B.unbind();
			proxy_B_2.unbind();
			
		
		} 
		catch (Exception exc) {
			exc.printStackTrace();
			log.error(exc);
		}
	}
}
